<?php
class CommentsController extends AppController {

	var $name = 'Comments';
	var $helpers = array('Html', 'Form', 'Text', 'Time');
	var $components = array('Captcha', 'Email','Security');
	//var $uses = array('Comment', 'User');
	var $paginate = array(
		'limit' => 60,
		'order' => array('Comment.created' => 'desc'),
		'conditions' => array('Comment.approved' => 1)
	);

	
	function securimage($random_number=null){
		//debug($this->captcha);
		$this->autoLayout = false; //a blank layout
		//override variables set in the component - look in component for full list
		$this->captcha->image_height = 80;
		$this->captcha->image_width = 350;
		$this->captcha->image_bg_color = '#eeeeee';
		$this->captcha->line_color = '#cccccc';
		$this->captcha->arc_line_colors = '#999999,#cccccc';
		$this->captcha->code_length = 5;
		$this->captcha->font_size = 28;
		$this->captcha->text_color = '#d7d7d7';
		$this->captcha->use_word_list = true;
		$this->captcha->wordlist_file = CONFIGS . 'captcha_words.txt';
		$this->set('captcha_data', $this->captcha->show()); //dynamically creates an image
	}

	    
	
	function admin_deletemany() {

	}
	
	function admin_index() {
		if(!empty($this->data)) {
			$todelete = array();
			foreach($this->data as $key => $item) {
				if($item['Comments']['delete'] == 1) {
					$this->Comment->del($key);
					$todelete[]=$key;
				}
					
			}
			$this->Session->setFlash(__('These comments successfully deleted:', true) . ' ' . implode(",", $todelete));
		}
		
		//Configure::load('comments');
		//$settings = Configure::read('Comment');
		//debug($this);
		$this->paginate['conditions'] = null;
		$this->Comment->recursive = 0;
		$this->set('comments', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Comment.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('comment', $this->Comment->read(null, $id));
	}
	
	function send_mail($comment) {
		
		debug(FULL_BASE_URL);
		$recipients = array(Configure::read('Comment.admin_mail'));
		$commenters = $this->Comment->findAllByRefUrl($comment['Comment']['ref_url']);
		foreach($commenters as $commenter) {
			$recipients []= $commenter['Comment']['email'];
		}
		$url = FULL_BASE_URL . $comment['Comment']['ref_url'];
		$message = Configure::read('Comment.body') . $url;
		$this->Email->from    = Configure::read('Comment.frommail');
		$this->Email->to      = $comment['Comment']['email'];
		$this->Email->subject = Configure::read('Comment.subject');
		$this->Email->lineLength = 300;
		$this->Email->send($message);
		
		$recipients = array_unique($recipients);
		
		foreach($recipients as $recipient) {
			if($recipient != $comment['Comment']['email']) {
				$this->Email->reset();
				$this->Email->from    = Configure::read('Comment.frommail');
				$this->Email->to      = $recipient;
				$this->Email->subject = Configure::read('Comment.subject_others');
				$this->Email->send(Configure::read('Comment.body_others') . $url);
			}
		}
		debug(Configure::read('Comment'));
		debug($this->Email);
		
	} 
	
	function embed() {
		
		$key = 'comments';
		//debug($_SESSION['securimage_code_value']);
		$redirect = $this->referer();
		if (!empty($this->data)) {
			//$key = "test";
			//clearCache('element_'. $key .'_commentscomments_count','views','');
			if( $this->captcha->check($this->data['Comment']['captcha_code']) == false ) {
				$this->Session->setFlash( __("Error: the captcha code was incorrect", true), null, null, 'comments');
			} else {
				//$this->data['Comment']['ref_url'] = $this->referer(); //$this->here;
				//Configure::load('comments');
				$settings = Configure::read('Comment');
				$this->data['Comment']['approved'] = $settings['default_approved'];
				$this->data['Comment']['ip'] = $_SERVER['REMOTE_ADDR'];
				$this->data['Comment']['agent'] = $_SERVER['HTTP_USER_AGENT'];
				/*
				if($user = $this->Auth->user())
					$this->data['Comment']['user_id'] = $user['User']['id'];
				*/
				
				$this->Comment->create();
				if ($this->Comment->save($this->data)) {
					//$this->send_mail($this->data);
					$this->Session->setFlash( __("The Comment has been saved", true), null, null, 'comments');
					$this->Session->del('comment_draft');
					$redirect = $redirect . '#comments';
				} else {
					$this->Session->setFlash( __("The Comment could not be saved. Please, try again.", true), null, null, 'comments_error');
					$this->Session->write('comment_draft', $this->data);
					$redirect = $redirect . '#comments-form';
				}
			}
			
		} 
		
		$this->redirect($redirect);
	}
	
	function latest($url = null) {
		//return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10));
		//debug($url);
		if($url != null) {
			$url = urldecode($url);
		} else {
			return array();
		}
		//debug($url);
		$params = array(
			'conditions' => array('Comment.ref_url' => $url,
			$this->paginate['conditions']), 
			'order' => $this->paginate['order']
		);
		return $this->Comment->find('all', $params);
		//$this->Comment->findAllByRefUrl($url);
	}
	
	function count($url = null) {
		if($url != null) {
			$url = urldecode($url);
			$params = array(
				'conditions' => array('Comment.ref_url' => $url,
				$this->paginate['conditions']), 
				'order' => $this->paginate['order']
			);
			//debug();
			return $this->Comment->find('count', $params);
		} else {
			return '0';
		}
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Comment->create();
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The Comment has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Comment could not be saved. Please, try again.', true));
			}
		}
		//$users = $this->Comment->User->find('list');
		
		//debug(FULL_BASE_URL . Router::url('', false));
		//debug(Router::url('/', true));
		/*
		$user = $this->Auth->user();
		$this->data['Comment']['email'] = $user['User']['email'];
		$this->data['Comment']['author'] = $user['User']['firstname'] . ' ' . $user['User']['lastname'];
		*/
		$this->data['Comment']['ref_url'] = $this->here;
		$this->data['Comment']['ip'] = $_SERVER['REMOTE_ADDR'];
		$this->data['Comment']['agent'] = $_SERVER['HTTP_USER_AGENT'];
		/*
		if($user = $this->Auth->user())
			$this->data['Comment']['user_id'] = $user['User']['id'];
			*/
		//$this->data['Tip']['approved'] = $tip_settings['default_approved'];
		//$this->set(compact('users'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Comment', true));
			$this->redirect('/admin/comments/');
		}
		if (!empty($this->data)) {
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The Comment has been saved', true));
				$this->redirect('/admin/comments/');
			} else {
				$this->Session->setFlash(__('The Comment could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Comment->read(null, $id);
		}
		//$users = $this->Comment->User->find('list');
		//$this->set(compact('users'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Comment', true));
			$this->redirect('/admin/comments/');
		}
		if ($this->Comment->del($id)) {
			$this->Session->setFlash(__('Comment deleted', true));
			$this->redirect('/admin/comments/');
		}
	}

}
?>